package aps.module_Core;

//buildcraft
import java.util.HashMap;
import java.util.Map;

import net.minecraft.src.Container;
import net.minecraft.src.ICrafting;
import net.minecraft.src.IInventory;
import net.minecraft.src.Packet;
import net.minecraft.src.TileEntity;
import aps.module_Core.module_Core.APSBlockTypes;
import buildcraft.core.DefaultProps;
import buildcraft.core.network.ISynchronizedTile;
import buildcraft.core.network.PacketPayload;
import buildcraft.core.network.PacketTileUpdate;
import buildcraft.core.network.PacketUpdate;
import buildcraft.core.network.TilePacketWrapper;
import buildcraft.core.proxy.CoreProxy;
//vanilla minecraft
//Froge
//Buildcraft
//APS


public abstract class TileEntityAPS extends TileEntity implements ISynchronizedTile
{	
	int[] texes = new int[6];
	int defTopTex; int defBottomTex; int defSideTex;
	
	private final TilePacketWrapper updatePacket;
	private final TilePacketWrapper descriptionPacket;

	public APSBlockTypes APSBlockType; 
	
	public int GuiID = 0; //default 0, no gui. 
	
	@SuppressWarnings("rawtypes")
	private static Map<Class, TilePacketWrapper> updateWrappers = new HashMap<Class, TilePacketWrapper>();
	
	@SuppressWarnings("rawtypes")
	private static Map<Class, TilePacketWrapper> descriptionWrappers = new HashMap<Class, TilePacketWrapper>();
	
	public TileEntityAPS(int BottomTex, int TopTex, int DefSideTex, APSBlockTypes type)
	{
		defBottomTex = BottomTex; texes[0] = defBottomTex;
		defTopTex = TopTex; texes[1] = defTopTex;
		defSideTex = DefSideTex; for(int i = 2; i < 6; i++) texes[i] = defSideTex;
		
		APSBlockType = type;
		
		if (!updateWrappers.containsKey(this.getClass()))
			updateWrappers.put(this.getClass(), new TilePacketWrapper(this.getClass()));
		
		if (!descriptionWrappers.containsKey(this.getClass()))
			descriptionWrappers.put(this.getClass(), new TilePacketWrapper(this.getClass()));
		
		updatePacket = updateWrappers.get(this.getClass());
		descriptionPacket = descriptionWrappers.get(this.getClass());
	}
	
	public int getTexture(int Face) {
		//if (this instanceof TileEntitySolarCollector && worldObj != null && worldObj.isRemote) System.out.println("Getting " + Face + " :" + texes[Face]);
		return texes[Face]; 
	}
	
	public void setTexture(int Face, int TexIndex) {
		//if (this instanceof TileEntitySolarCollector && worldObj.isRemote) System.out.println("Setting " + Face + " :"+ texes[Face]);
		if(Face < 0 || Face > 5) return;
		texes[Face] = TexIndex; 
	}
	
	public void resetTexture(int Face)
	{
		if(Face < 0 || Face > 5) return;
		if(Face == 0) texes[Face] = defBottomTex;
		else if(Face == 5) texes[Face] = defTopTex;
		else texes[Face] = defSideTex;
	}
	public void resetAllTextures()
	{
		texes[0] = defBottomTex;
		texes[1] = defTopTex;
		for(int i = 2; i < 6; i++) texes[i] = defSideTex;
	}
	
	public abstract void kill();

	public IInventory getInventory() { return null; } //changed from abstract, not all machines need this
	
	public boolean hasGUI = false;

	//public abstract GuiScreen getGUI(EntityPlayer entityplayer);
	
	public void sendNetworkUpdate()
	{
		if(CoreProxy.proxy.isSimulating(worldObj))
			CoreProxy.proxy.sendToPlayers(getUpdatePacket(), worldObj, xCoord, yCoord, zCoord, DefaultProps.NETWORK_UPDATE_RANGE);
	}
	
	public Packet getAuxillaryInfoPacket()
	{
		return new PacketTileUpdate(this).getPacket();
	}
	
	public PacketPayload getPacketPayload()
	{
		return updatePacket.toPayload(this);
	}
	
	public Packet getUpdatePacket()
	{
		return new PacketTileUpdate(this).getPacket();
	}
	
	public void handleDescriptionPacket(PacketUpdate packet)
	{
		updatePacket.fromPayload(this, packet.payload);
	}
	
	public void handleUpdatePacket(PacketUpdate packet)
	{
		updatePacket.fromPayload(this, packet.payload);
	}
	
	public void postPacketHandling(PacketUpdate packet)
	{
		
	}
	
	public void sendGUIUpdates(Container container, ICrafting iCrafting)
	{
		
	}
	
	public void getGUIUpdates(int channel, int value)
	{
		
	}
}